﻿using LBSoft.Business;
using LBSoft.Business.Model;
using LBSoft.Business.NewDAL;
using LBSoft.Common.DotNetBean;
using LBSoft.Common.DotNetCode;
using LBSoft.Common.DotNetConfig;
using LBSoft.Common.DotNetData;
using LBSoft.Common.DotNetEncrypt;
using LBSoft.Common.DotNetExt;
using LBSoft.Common.DotNetJson;
using LBSoft.Common.DotNetUI;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;

namespace LBSoft.HplusAdmin.Business.Common
{
    /// <summary>
    /// Common 的摘要说明
    /// </summary>
    public class Common : IHttpHandler
    {
        HttpContext con = null;
        mJsonResult json = new mJsonResult();
        Common_Dal systemidao = new Common_Dal();
        LogHelper log = new LogHelper();

        public void ProcessRequest(HttpContext context)
        {
            con = context;

            switch (con.Request["PostType"])
            {
                case "list": LoadList(); break;
                case "get": GetData(); break;
                case "set": SetData(); break;
            }
        }

        #region 列表
        private void LoadList()
        {
            switch (con.Request["ActionType"])
            {
                case "Roles": LoadRolesList(); break; //角色下拉框
            }
        }

        /// <summary>
        /// 角色下拉框
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void LoadRolesList()
        {
            int count = 0;
            int pageSize = Int32.MaxValue; // con.Request["rows"].ToInt32(Int32.MaxValue);
            int curPage = con.Request["pageNumber"].ToInt32(1);
            StringBuilder SqlWhere = new StringBuilder(WebHelper.GetCondition(con.Request));
            SqlWhere.Append(" and IsDelete=0 ");
            IList<SqlParam> IList_param = new List<SqlParam>();
            string tables = @" SYS_Roles a ";
            string cols = " a.* ";
            string orderBy = " Sort ASC ";
            DataTable dt = systemidao.GetListByPage(tables, cols, SqlWhere, orderBy, IList_param, curPage, pageSize,
                ref count);

            PageSelectData psJson = new PageSelectData();
            if (dt != null)
            {
                psJson.list = DataTableHelper.DataTableToArrayList(dt);
                psJson.totalRow = count;
            }
            con.Response.Write(JsonConvert.SerializeObject(psJson));
        }
        #endregion

        #region 获取
        private void GetData()
        {
            switch (con.Request["ActionType"])
            {
                case "Nation": GetNationByCode(); break; //获取民族名称
            }
        }

        /// <summary>
        /// 获取民族名称
        /// </summary>
        private void GetNationByCode()
        {
            string nationCode = con.Request["NationCode"].ToString2();
            string name = UtilsDal.GetNationName(nationCode);
            con.Response.Write(name);
        }
        #endregion

        #region 编辑
        private void SetData()
        {
            throw new NotImplementedException();
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}